uniform sampler2D normalOutline;
uniform sampler2D textureMap;

uniform vec2 offset[9];
uniform int kernel[9];

void main() {

        float sum = 0.0;
        int i;
          
        vec4 centerColor = texture2D(textureMap, gl_TexCoord[0].st);

        for (i=0 ; i<9; i++){
            vec4 color = texture2D(textureMap, gl_TexCoord[0].st + offset[i]);
            float dist = length(color - centerColor);
            sum += dist*kernel[i];
        } 
        
        vec4 filterResult;
	float thres = 0.05f; // up for fine tuning
	if (sum > thres){ // is edge
		filterResult = vec4(1.0f, 1.0f, 1.0f, 1.0f);
        } else { 
                filterResult = vec4(0.0f, 0.0f, 0.0f, 1.0f);
        }
        
        gl_FragColor = filterResult + texture2D(normalOutline, gl_TexCoord[0].st);
}
